<?php

namespace Frieza\Modules\Position\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\MemberHistoryNick;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;

//HistoryNickController 昵称修改记录相关类
class HistoryNickController extends Controller
{

    const PAGE_SIZE = 20;

    //index 昵称修改记录入口
    public function index(Request $request, Content $content)
    {

        $page = $request->input('page', 1);
        $pageSize = $request->input('per_page', 20);
        $search = $request->input();

        $query = [];
        if (isset($search['uid']) && is_numeric($search['uid'])) {
            $query['uid'] = $search['uid'];
        }
        if (!empty($search['history_nick'])) {
            $query['history_nick'] = $search['history_nick'];
        }
        if (!empty($search['edittime'])){
            $query["start"] = $search["edittime"]["start"];
            $query["end"] = $search["edittime"]["end"];
        }

        //调用获取数据的方法
        $infos = MemberHistoryNick::getListByCondition($query, $page, $pageSize);
        $gridModel = new pGridDataModel($infos["list"], $infos["counts"], ['perPage' => $pageSize]);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {

            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->disableColumnSelector();
            $grid->filter(function ($filter){

                $filter->disableIdFilter();
                $filter->column(8, function ($filter){
                    $filter->equal('uid', 'uid')->placeholder("请输入UID");
                    $filter->equal('history_nick', '历史昵称')->placeholder("请输入历史昵称");

                });
                $filter->column(12, function ($filter){
                    $filter->between("edittime", "更改时间")->datetime();
                });

                $filter->expand();
            });

            $grid->column('id');
            $grid->column("uid", "用户ID")->display(function() {
                return "<a href='/admin/user/Detail/$this->uid' target='_blank' >".$this->uid."</a>";
            })->width(100);;
            $grid->column("history_nick", "历史昵称");
            $grid->column("isadmin", "是否可用")->display(function(){
                $day = (time() - $this->edittime) / 86400;
                if ($day > 30 || $this->isadmin) {
                    return 1;
                }
                return 0;
            })->bool();
            $grid->column("new_nick", "新昵称");
            $grid->column("edittime", "修改时间")->display(function(){
                return date("Y-m-d H:i:s", $this->edittime);
            });

        });

        $tab = new Tab();
        $tab->add('昵称修改记录列表', $listGrid->render());
        $content->row($tab);
        return $content
            ->header('昵称修改记录')
            ->breadcrumb(['text' => '昵称修改记录']);
    }
}
